﻿using System;
using System.Globalization;
using System.Web.UI;
using StockholmsStad.Web.UI.Class.Controller;
using StockholmsStad.Web.UI.Class.Facade;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI.VarBokning
{
    /// <summary>
    /// Webbsida för att se och avboka vår bokning
    /// </summary>
    public partial class VarBokning : Page, IVarBokning
    {
        private static readonly DateTimeFormatInfo FORMAT_INFO = new CultureInfo("sv-SE", false).DateTimeFormat;
        private OurBookingController _controller;
        private BookingModel _bookingModel;

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpSessionAdapter session = new HttpSessionAdapter(Session);
            WebServiceFacade facade = new WebServiceFacade();
            _bookingModel =  new BookingModel(session, facade);
            _controller = new OurBookingController(_bookingModel,
                                         new LoginModel(session, facade, new HttpContextAdapter(Context)),
                                         this);
            _controller.PageLoad();

            if(!Page.IsPostBack)
            {
                btnCancelBooking.Enabled =
                    btnChangeBooking.Enabled = _controller.HasBooking && _bookingModel.IsUpdateable();; 
            }
        }

        #region IVarBokning Members

        /// <summary>
        /// Uppdaterar historikloggen
        /// </summary>
        /// <param name="historyList"></param>
        public void SetHistory(History[] historyList)
        {
            // Utökar listboxens rader eftersom det är en omkringliggande div som hanterar scrollbars
            repHistory.DataSource = historyList;
            repHistory.DataBind();
        }

        /// <summary>
        /// Formaterar loggmeddelanden
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="user"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public string FormatLog(DateTime dateTime, string user, string description)
        {
            return string.Format("{0:yyyy-MM-dd HH:mm} {1} {2}", dateTime, user, description);
        }

        /// <summary>
        /// Uppdaterar sidan med bokningsinformation
        /// </summary>
        /// <param name="booking"></param>
        /// <param name="locationName"></param>
        public void UpdateBooking(Booking booking, string locationName)
        {
            bookingSummary.UpdateBooking(booking, GetDateWeekdayText, GetTimeText,
                                         locationName);
        }

        #endregion


        private static string GetTimeText(DateTime dateTime)
        {
            return dateTime.ToString("HH:mm");
        }

        private static string GetDateWeekdayText(DateTime dateTime)
        {
            return dateTime.ToString("dddd d MMMM yyyy", FORMAT_INFO);
        }

        /// <summary>
        /// Användaren trycker på Avboka
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancelBooking_Click(object sender, EventArgs e)
        {
            _controller.CancelBooking();
            Response.Redirect("~/VarBokning/VarBokning.aspx");
        }

        /// <summary>
        /// Användaren trycker på Ändra
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChangeBooking_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Bokning/Bokning.aspx");
        }
    }
}